import 'package:HnXG/Api.dart';
import 'package:HnXG/DIalog.dart';
import 'package:HnXG/DIalog.dart';
import 'package:HnXG/Request/Request3.dart';
import 'package:HnXG/Request/TaskAgvRequest.dart';
import 'package:HnXG/pages/Pages/NewWarehousePage.dart';
import 'package:HnXG/snackbar.dart';
import 'package:flutter/material.dart';

class ProductionPageLinePage  extends StatefulWidget {
    const ProductionPageLinePage ({super.key});
  @override
  State<StatefulWidget> createState() => _productionPageLinePageState();
}

class _productionPageLinePageState extends State<ProductionPageLinePage> {
  @override
  // 存储仓库占用状态的列表
  List<bool> warehouseOccupancy = List.filled(4, false);
  List<String> warehousePositionCode=List.filled(2, ''); 
      @override
  void initState() {
    super.initState();
    fetchWarehouseStatus();
  }
    // 异步方法，通过 Api 类的 GetView_PositionState 接口获取仓库状态
  Future<void> fetchWarehouseStatus() async {
    try {
      final response = await Api.GetView_PositionState(Request3(Type: "产线"));
    
      if (response!= null && response['dto'] != null) {
        setState(() {
          for (int i = 0; i < 2; i++) {
            warehouseOccupancy[i] = response['dto'][i]['isOccupy'];
           
            warehousePositionCode[i]=(response['dto'][i]['positionCode']);
           
          }
        });
      }
    } catch (e) {
     
    }
  }

  // 主页面布
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.blue, // 新增背景色
        title: const Text('缓存取车',
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            shadows: [Shadow(color: Colors.black38, offset: Offset(1,1), blurRadius: 3)],
          )
        ),
        centerTitle: true, // 新增标题居中
        elevation: 3, // 新增阴影效果
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // 仓库进缓存1区域
              Container(
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.white, width: 0),
                  borderRadius: BorderRadius.circular(8)
                ),
                child: Column(
                  children: [
                    const Text('缓存取车', style: TextStyle(fontWeight: FontWeight.bold)),
                    const SizedBox(height: 8),
                    Wrap(
                      spacing: 8,
                      children: [
                        _buildCacheButton('缓存——>虚拟位','','',context),
                        // _buildCacheButton('缓存1——>产线2','Loc-5-1','Loc-8-1',context),
                      ],
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 20),
              // 仓库进缓存2区域
              // Container(
              //   padding: EdgeInsets.all(10),
              //   decoration: BoxDecoration(
              //     border: Border.all(color: Colors.grey, width: 1),
              //     borderRadius: BorderRadius.circular(8)
              //   ),
              //   child: Column(
              //     children: [
              //       const Text('缓存2取车', style: TextStyle(fontWeight: FontWeight.bold)),
              //       const SizedBox(height: 8),
              //       Wrap(
              //         spacing: 8,
              //         children: [
              //           _buildCacheButton('缓存2——>产线1','Loc-6-1','Loc-7-1',context),
              //           // _buildCacheButton('缓存2——>产线2','Loc-6-1','Loc-8-1',context),
              //         ],
              //       ),
              //     ],
              //   ),
              // ),
              // 状态展示区域
              const SizedBox(height: 20),
              // Container(
              //   padding: EdgeInsets.all(10),
              //   decoration: BoxDecoration(
              //     border: Border.all(color: Colors.grey, width: 1),
              //     borderRadius: BorderRadius.circular(8)
              //   ),
              //   child: Column(
              //     children: [
              //       const Text('产线状态', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
              //       const SizedBox(height: 16),
              //       Row(
              //         mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              //         children: [
              //           // _buildCacheStatusIndicator('产线1', warehouseOccupancy[0],warehousePositionCode[0],context), // 后期替换布尔值
              //           // _buildCacheStatusIndicator('产线2', warehouseOccupancy[1],warehousePositionCode[1],context), // 后期替换布尔值
              //         ],
              //       ),
              //     ],
              //   ),
              // ),
          ],
        ),
      ),
    ));
  }


  /// 缓存按钮样式
  Widget _buildCacheButton(String text,String origination_Code,String target_Code,BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        backgroundColor: Colors.blue, 
        minimumSize: const Size(120, 40), // 增大按钮尺寸
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
          side: BorderSide(color: Colors.grey, width: 1) // 添加按钮边线
        ),
      ),

      
      onPressed: () async {

  final result = await InfoDialog.show(context, "提示信息", "您确定要下发任务吗？");
      if(result==true){
        
           final response=await Api.CreateFromCachaTaskOrder();
   
   if(response['state']==true){
   SuccessSnackBar.show(context, message: response['msg']);
   }
   else{
    ErrorSnackBar.show(context, message: response['msg']);
   }
        
        }

 
      }, // 预留点击事件
      child: Text(text, style: const TextStyle(color: Colors.white, fontSize: 14)), // 减小文字大小
    );
  }


}


/// 缓存状态指示器
Widget _buildCacheStatusIndicator(String name, bool isOccupied,String? positionCode,BuildContext context) {



  return GestureDetector(
    onTap: () async {
      // 这里可以添加点击后的具体逻辑
       final result = await InfoDialog.show(context, "提示信息", "您确定要释放库位吗？");
    },
    child: Column(
      
   
    children: [
      Stack(
        alignment: Alignment.center,
        
        children: [
          Container(
            width: 100,
            height: 100,
            decoration: BoxDecoration(
              color: isOccupied ? Colors.red : Colors.green,
              borderRadius: BorderRadius.circular(10),
          
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.2),
                  spreadRadius: 2,
                  blurRadius: 5,
                  offset: const Offset(0, 3),
                ),
              ],
            ),
          ),
          Text(
            isOccupied ? '占用' : '空闲',
            style: const TextStyle(color: Colors.white, fontSize: 16),
          ),
          const Positioned( // 新增定位组件
            bottom: 5,  // 距离底部5像素
            left: 38,    // 距离左侧5像素
            child:Icon(Icons.cleaning_services_sharp, color: Colors.black, size: 25,)
      )],
        ),
      const SizedBox(height: 8),
      Text(name, style: const TextStyle(fontSize: 16)),
    ],
  ));
}